package com.upb.webserver.fisco.stat;

import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.rest.fisco.FiscoStatRestService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

import static org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME;

/**
 * request hyperledger-stat for data
 */
@Log4j2
@RestController
@RequestMapping("fisco-stat")
public class FiscoStatController {

    @Autowired
    private FiscoStatRestService fiscoStatRestService;

    @GetMapping("groupBasicData")
    public BasePageResponse getFiscoGroupBasicData(
            @RequestParam Integer chainId,
            @RequestParam Integer frontId,
            @RequestParam(required = false) Integer groupId,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer pageNumber,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime beginDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime endDate) {
       return  fiscoStatRestService.getGroupBasicData(chainId, frontId,
                beginDate, endDate, pageNumber, pageSize, groupId);
    }

    @GetMapping("/metrics/nodeMonitor")
    public BasePageResponse getFiscoNodeMonitor(
            @RequestParam Integer chainId,
            @RequestParam Integer frontId,
            @RequestParam(required = false) Integer groupId,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime beginDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime endDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime contrastBeginDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime contrastEndDate,
            @RequestParam(required = false, defaultValue = "1") int gap) {
        return fiscoStatRestService.getNodeMonitor(chainId, frontId,
                beginDate, endDate, contrastBeginDate, contrastEndDate, gap, groupId);
    }

    @GetMapping("/metrics/serverPerformance")
    public BasePageResponse getFiscoPerformanceList(
            @RequestParam Integer chainId,
            @RequestParam Integer frontId,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime beginDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime endDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime contrastBeginDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DATE_TIME) LocalDateTime contrastEndDate,
            @RequestParam(required = false, defaultValue = "1") int gap) {
        return fiscoStatRestService.getServerPerformance(chainId, frontId,
                beginDate, endDate, contrastBeginDate, contrastEndDate, gap);
    }

}
